import "./reg.html";
import { Session } from 'meteor/session';

import i18n from 'meteor/universe:i18n';

function regCallBack(error){
  if (error) {
            console.log(error);
            if (error.reason == "Username already exists.") {
              FlashMessages.sendError("用户名已经被占用");
              
              return false;
            }
            if (error.reason == "Password may not be empty") {
              FlashMessages.sendError("密码不得为空");
              
              return false;
            }
            if (error.error == "403") {
            FlashMessages.sendError("此邮箱已经被注册了");
            return false;
           }
           return false;
          }
          FlowRouter.go('App.login');
          FlashMessages.sendSuccess("注册成功,可以登录了");
}

Template.reg.onCreated(function () {
  Meteor.subscribe('userData');
});

Template.reg.helpers({
  counter() {
    return Template.instance().counter.get();
  },
});

Template.reg.events({
  'submit form'(event, instance) {
    
  	event.preventDefault();

    var user = Meteor.users.find();



  	let username_jquery = "[name='username']";
  	let mobile_jquery = "[name='mobile']";
  	let password_jquery = "[name='password']";
  	let password_repeat_jquery = "[name='password-repeat']";
  	let username = $(username_jquery).val();
  	let mobile = $(mobile_jquery).val();

  	let password = $(password_jquery).val();
  	let password_repeat = $(password_repeat_jquery).val();
    let valid_text = $("#valid_text").val();


  	if (password != password_repeat) {
  		
      FlashMessages.sendError("两次密码不一致");
  		return false;
  	}
    if (mobile == '') {
      FlashMessages.sendError("手机号不得为空");
      return false;
    }
    
    

    if (valid_text == '') {
      FlashMessages.sendError("请输入手机验证码");
      return false;
    }
    if (valid_text != Session.get("mobileValid") ) {
       FlashMessages.sendError("手机验证码错误");
      return false;
    }

    
    Meteor.subscribe("MobileUserName", mobile, {onReady: function(){
      if( Meteor.users.find({profile: {mobile: mobile}}).count() > 0){
        FlashMessages.sendError("手机号已经存在");
        return false;
      }
      let newMemberId = Accounts.createUser({
            mobile: mobile,
            username: username,
            password: password,
            profile: {
              mobile: mobile
            },
        }, (error) => regCallBack(error));
    }});
  	
  	
    
  },

  'click #sms'(event, instance) {
    let mobile_jquery = "[name='mobile']";
    let mobile = $(mobile_jquery).val();
    Meteor.subscribe("MobileUserName", mobile, {onReady: function(){
      if( Meteor.users.find({profile: {mobile: mobile}}).count() > 0){
        FlashMessages.sendError("手机号已经存在");
        return false;
      }else{
         Meteor.call('user.getMobileValid', (error, result) => {
        if (error) {
          alert(error);
          return false;
        } else {
          Session.set("mobileValid", result);
          let mobileValid = Session.get("mobileValid");
          sendsms(mobileValid, $("#valid_mobile").val());

        }
      });

      }
    }});


    

  },

});


